package com_.dxy_test_.bit_;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description 测试位运算
 * @Author dangxianyue
 * @Date 2023/6/20 16:43
 */
public class BitTest {

    /**ReentrantReadWriteLock设计===========================*/
    static final int SHARED_SHIFT   = 16;
    static final int SHARED_UNIT    = (1 << SHARED_SHIFT);
    static final int MAX_COUNT      = (1 << SHARED_SHIFT) - 1;
    static final int EXCLUSIVE_MASK = (1 << SHARED_SHIFT) - 1;

    /** Returns the number of shared holds represented in count  */
    static int sharedCount(int c)    { return c >>> SHARED_SHIFT; }
    /** Returns the number of exclusive holds represented in count  */
    static int exclusiveCount(int c) { return c & EXCLUSIVE_MASK; }
    /**ReentrantReadWriteLock设计=============================*/


    /**ThreadPoolExecutor设计=============================*/
    private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
    private static final int COUNT_BITS = Integer.SIZE - 3;
    private static final int CAPACITY   = (1 << COUNT_BITS) - 1;

    // runState is stored in the high-order bits
    private static final int RUNNING    = -1 << COUNT_BITS;
    private static final int SHUTDOWN   =  0 << COUNT_BITS;
    private static final int STOP       =  1 << COUNT_BITS;
    private static final int TIDYING    =  2 << COUNT_BITS;
    private static final int TERMINATED =  3 << COUNT_BITS;

    // Packing and unpacking ctl
    private static int runStateOf(int c)     { return c & ~CAPACITY; }
    private static int workerCountOf(int c)  { return c & CAPACITY; }
    private static int ctlOf(int rs, int wc) { return rs | wc; }
    /**ThreadPoolExecutor设计=============================*/


    public static void main(String[] args) {
        System.out.println("RUNNING: " + RUNNING + " , 二进制：" + Integer.toBinaryString(RUNNING));
        System.out.println("SHUTDOWN: " + SHUTDOWN + " , 二进制：" + Integer.toBinaryString(SHUTDOWN));
        System.out.println("STOP: " + STOP + " , 二进制：" + Integer.toBinaryString(STOP));
        System.out.println("TIDYING: " + TIDYING + " , 二进制：" + Integer.toBinaryString(TIDYING));
        System.out.println("TERMINATED: " + TERMINATED + " , 二进制：" + Integer.toBinaryString(TERMINATED));
    }
}
